package com.qike.duola.service.impl;

import com.alibaba.fastjson.JSON;
import com.qike.duola.socket.NewPlanOrderSocket;
import com.qike.duola.socket.NewTransLineSocket;
import com.qike.duola.service.DuolaLineService;
import com.qike.duola.service.DuolaPlanService;
import com.qike.duola.service.SocketService;
//import com.qike.duola.socket.NewTransOrderSocket;
import com.qike.duola.socket.NewTransOrderSocket;
import com.qike.duola.vo.LineListVo;
import com.qike.duola.vo.PlanListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/17 17:50
 */

@Service
public class NewTransLineSocketImpl implements SocketService {

    @Autowired
    private DuolaLineService duolaLineService;

    @Autowired
    private DuolaPlanService duolaPlanService;



    @Override
    public void sendNewSocket(Class T, String value) {
        try {
            //获取方法
            Method printMethod = T.getMethod("sendMessage",String.class);
            //调用
            printMethod.invoke(T.newInstance(),value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送新生产线列表
     */
    @Async
    @Override
    public void sendNewTransLineSocket() {
        List<LineListVo> list = duolaLineService.lineList();
        this.sendNewSocket(NewTransLineSocket.class, JSON.toJSONString(list));
    }

    /**
     * 发送新生产计划列表
     */
    @Override
    @Async
    public void sendNewPlanListSocket() {
        List<PlanListVo> listVos = duolaPlanService.planList(null,null, null);
        this.sendNewSocket(NewPlanOrderSocket.class, JSON.toJSONString(listVos));
    }

    /**
     * 发送新运输单列表
     */
    @Override
    @Async
    public void sendNewTransOrderListSocket() {
        this.sendNewSocket(NewTransOrderSocket.class, null);
    }

}
